﻿using AIStudio.Core;
using AIStudio.Wpf.AvalonDockMVVM.ViewModels;
using AIStudio.Wpf.BasePage.Events;
using AIStudio.Wpf.BasePage.Models;
using AIStudio.Wpf.HomePage.Helpers;
using CommonServiceLocator;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;


namespace AIStudio.Wpf.HomePage.ViewModels
{
    class MainTabViewModel : Prism.Mvvm.BindableBase
    {
        public DockManagerViewModel DockManagerViewModel { get; private set; }
        IEventAggregator _eventAggregator { get; }
        ILocalSetting _localSetting { get; }
        public MainTabViewModel()
        {
            _eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            _eventAggregator.GetEvent<MenuClickEvent>().Subscribe(MenuClickEventReceived, ThreadOption.PublisherThread, false, item => !string.IsNullOrEmpty(item.Label));
            _eventAggregator.GetEvent<OpenEditEvent>().Subscribe(OpenEditEventReceived);
            _localSetting = ServiceLocator.Current.GetInstance<ILocalSetting>();

            var viewModels = new List<DockWindowViewModel>();
            viewModels.Add(new HomeViewModel() { Title = "首页", CanClose = false });
            DockManagerViewModel = new DockManagerViewModel(viewModels);
            DockManagerViewModel.SelectedDocumentChanged += DockManagerViewModel_SelectedDocumentChanged;
        }

        private void DockManagerViewModel_SelectedDocumentChanged(DockWindowViewModel obj)
        {
            _localSetting.PageTitle = obj.Title + " - " + LocalSetting.Title;
        }

        private void MenuClickEventReceived(AMenuItem obj)
        {
            SelectedMenuItem = obj;
        }

        private AMenuItem selectedMenuItem;
        public AMenuItem SelectedMenuItem
        {
            get { return selectedMenuItem; }
            set
            {
                selectedMenuItem = value;
                SelectedMenuItemChanged(value);
            }
        }

        void SelectedMenuItemChanged(AMenuItem item)
        {
            if (item == null || string.IsNullOrEmpty(item.Code))
            {
                return;
            }

            Type type = null;
            if (item.Type == 1)
            {
                type = AssemblyHelper.DockWindowViewModelTypes.FirstOrDefault(p => p.Name == item.Code || item.WpfCode.EndsWith("/" + p.Name.Replace("ViewModel", "") + "/"));
            }
            if (type != null)
            {
                DockWindowViewModel obj = DockManagerViewModel.GetDockWindowViewModel(item.Label);
                if (obj == null)
                {
                    obj = System.Activator.CreateInstance(type) as DockWindowViewModel;       // 创建实例
                    obj.Title = item.Label;
                    DockManagerViewModel.AddDockWindowViewModel(obj);
                }
                else
                {
                    obj.IsSelected = true;
                }
                _localSetting.PageTitle = obj.Title + " - " + LocalSetting.Title;
            }
        }

        void OpenEditEventReceived(Tuple<AMenuItem, object[]> tuple)
        {
            var item = tuple.Item1;
            object[] id = tuple.Item2;
            if (item == null || string.IsNullOrEmpty(item.Code))
            {
                return;
            }

            Type type = null;
            if (item.Type == 1)
            {
                type = AssemblyHelper.DockWindowViewModelTypes.FirstOrDefault(p => p.Name == item.Code || item.WpfCode.EndsWith("/" + p.Name.Replace("ViewModel", "") + "/"));
            }

            if (type != null)
            {
                object obj = DockManagerViewModel.GetDockWindowViewModel(item.Label);
                if (obj == null)
                {
                    obj = System.Activator.CreateInstance(type, new object[] { id });       // 创建实例
                }
                var methodInfo = type.GetMethod("EditById");
                methodInfo.Invoke(obj, new object[] { id });
            }
        }
    }
}
